import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';
import 'package:vape_xe/values/values.dart';
import 'package:vape_xe/widgets/widgets.dart';

class NicknameDialogView extends Dialog {
  late TextEditingController nickNameController;

  NicknameDialogView({super.key, required String nickName}) {
    nickNameController = TextEditingController(text: nickName);
  }

  Future<void> updateNickName() async {
    if (nickNameController.text.isEmpty) {
      toast('Nickname empty'.tr);
      return;
    }
    Loading.show();
    await Request().post(
      '/user/editUser',
      {'nickName': nickNameController.text},
    ).then((_) {
      toast('Success');
      EventService.to.sendMessage(
        '${EventService.EventUpdate}_${EventService.ModuleNickname}_${nickNameController.text}',
        false,
      );
      Navigator.pop(Get.context!);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Center(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 32.h),
          margin: EdgeInsets.symmetric(horizontal: 48.w),
          decoration: BoxDecoration(
            color: Colors.black,
            borderRadius: 20.radius,
            boxShadow: [Shadows.dialogShadow],
          ),
          child: Row(
            children: [
              Expanded(
                child: InnerContainerWidget(
                  margin: EdgeInsets.only(left: 32.w, right: 12.w),
                  child: Padding(
                    padding: EdgeInsets.only(left: 16.w),
                    child: TextField(
                      controller: nickNameController,
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: 'Nickname'.tr,
                      ),
                      style: TextStyle(color: Colors.white, fontSize: 14.sp, fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
              ),
              GestureDetector(
                onTap: () => updateNickName(),
                child: Container(
                  width: 88.w,
                  height: 52.h,
                  margin: EdgeInsets.only(right: 32.w),
                  decoration: BoxDecoration(
                    gradient: const LinearGradient(colors: [Color(0xff6EDEF7), Color(0xff3351EF)]),
                    borderRadius: 12.radius,
                  ),
                  child: Center(
                    child: Text(
                      'Update'.tr,
                      style: const TextStyle(fontFamily: 'Montserrat', fontWeight: FontWeight.bold),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
